//
//  WashTheWardrobeNearViewTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/4.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "WashTheWardrobeNearViewTableViewCell.h"
#import <CoreLocation/CoreLocation.h>

@interface WashTheWardrobeNearViewTableViewCell ()

Strong UIImageView *titleImageView;
Strong UILabel *addressLabel;
Strong UILabel *nameLabel;
Strong UILabel *addressInformationLabel;
Strong UIView *lineView;
Strong UILabel *contentLabel;
Strong UIView *line1View;

@end

@implementation WashTheWardrobeNearViewTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleImageView = [UIImageView new];
        _titleImageView.layer.masksToBounds = YES;
        _titleImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        
        _addressLabel  = [[UILabel alloc]init];
        _addressLabel.font = DEF_FontSize_16;
        _addressLabel.textColor = getColor(mainColor);
        _addressLabel.textAlignment = NSTextAlignmentRight;
        
        _nameLabel  = [[UILabel alloc]init];
        _nameLabel.font = DEF_FontSize_14;
        _nameLabel.textColor = getColor(mainColor);
        
        _addressInformationLabel  = [[UILabel alloc]init];
        _addressInformationLabel.font = DEF_FontSize_14;
        _addressInformationLabel.textColor = getColor(mainColor);
        _addressInformationLabel.layer.masksToBounds = YES;
        _addressInformationLabel.layer.cornerRadius = 25 / WIDTH_5S_SCALE / 2;
        _addressInformationLabel.backgroundColor = getColor(@"eeeeee");
        
        _contentLabel  = [[UILabel alloc]init];
        _contentLabel.font = DEF_FontSize_14;
        _contentLabel.textColor = getColor(textColor);
        _contentLabel.numberOfLines = 0;
        
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:_lineView];
        
        _line1View = [[UIView alloc]init];
        _line1View.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:_line1View];
        
        [self.contentView sd_addSubviews:@[_titleImageView , _nameLabel , _addressLabel , _addressInformationLabel , _contentLabel]];
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(27 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(63 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(66 / WIDTH_5S_SCALE);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(33 / WIDTH_5S_SCALE);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.right.mas_equalTo(0);
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(0 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(33 / WIDTH_5S_SCALE);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(0 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(80 / WIDTH_5S_SCALE);
    }];
    
    [self.addressInformationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(- 12 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(25 / WIDTH_5S_SCALE);
    }];
    
    [self.line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 0 / WIDTH_5S_SCALE, 0.5));
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.titleImageView.image = [UIImage imageNamed:@"004"];
    self.nameLabel.text = model[@"deviceName"];
    double address = [self distanceBetweenOrderBy:[[UserModel sharedInstanced].latitude doubleValue ]:[model[@"eLatitude"] doubleValue] :[[UserModel sharedInstanced].longitude doubleValue]:[model[@"eLongitude"] doubleValue]];
    self.addressLabel.text = [NSString stringWithFormat:@"%dm",(int)address];
    self.contentLabel.attributedText = [self getAttributeName:model[@"cabinet_total"] second:model[@"total_used"] three:model[@"unused_total"] four:@"20"];
    self.addressInformationLabel.text = [NSString stringWithFormat:@"   %@",model[@"cabinet_adderss"]];
}

- (NSAttributedString *)getAttributeName:(NSString *)one second:(NSString *)second three:(NSString *)three four:(NSString *)four{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *oneStr = [[NSAttributedString alloc]initWithString:one attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *secondAtt = [[NSAttributedString alloc]initWithString:second attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    NSAttributedString *threeStr = [[NSAttributedString alloc]initWithString:three attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
//    NSAttributedString *fourAtt = [[NSAttributedString alloc]initWithString:four attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"洗衣柜的总数量  "]];
    [attStr appendAttributedString:oneStr];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"已用洗衣柜数量  "]];
    [attStr appendAttributedString:secondAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"剩余洗衣柜数量  "]];
    [attStr appendAttributedString:threeStr];
//    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
//    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"故障洗衣柜数量  "]];
//    [attStr appendAttributedString:fourAtt];
//    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}



- (double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance ;
    
}

@end
